package view.componentes;

import java.util.Iterator;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import javax.swing.AbstractListModel;

public class GenericListModel<T> extends AbstractListModel<Object> {

	private static final long serialVersionUID = 1L;
	
	SortedSet<T> lista;

	public GenericListModel() {
		lista = new TreeSet<T>();
	}

	public int getSize() {
		return lista.size();
	}

	@SuppressWarnings("unchecked")
	public T getElementAt(int index) {
		return (T) lista.toArray()[index];
	}

	public void add(T element) {
		if (lista.add(element)) {
			fireContentsChanged(this, 0, getSize());
		}
	}

	public void addAll(Set<T> elements) {
		lista.addAll(elements);
		fireContentsChanged(this, 0, getSize());
	}

	public void clear() {
		lista.clear();
		fireContentsChanged(this, 0, getSize());
	}

	public boolean contains(T element) {
		return lista.contains(element);
	}

	public T firstElement() {
		return lista.first();
	}

	public Iterator<T> iterator() {
		return lista.iterator();
	}

	public T lastElement() {
		return lista.last();
	}

	public boolean removeElement(T element) {
		boolean removed = lista.remove(element);
		if (removed) {
			fireContentsChanged(this, 0, getSize());
		}
		return removed;
	}
	
	/**
	 * @return the lista
	 */
	public Set<T> getLista() {
		return ((Set<T>)lista);
	}
	
}
